package com.stars.services.impl.system;

import com.stars.common.base.BaseMsg;
import com.stars.common.base.ResultMsg;
import com.stars.common.maps.JsonUtils;
import com.stars.dao.mapper.system.SysRedisMapper;
import com.stars.entity.dto.system.SysRedisDTO;
import com.stars.entity.system.SysRedisEntity;
import com.stars.services.impl.BaseConfigServiceImpl;
import com.stars.services.system.SysRedisConfigServices;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Title: 系统创建的redis 业务逻辑
 * @author dj
 * @date 2019-10-21 22:32:59
 * @version V1.0
 *
 */
@Service
public class SysRedisConfigServicesImpl  extends BaseConfigServiceImpl<SysRedisMapper , SysRedisEntity> implements SysRedisConfigServices {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private SysRedisMapper sysRedisMapper;

    @Override
    public ResultMsg set(String key, String obj) throws Exception {
        set(key,obj,7,TimeUnit.DAYS);
        return ResultMsg.success();
    }
    @Override
    public ResultMsg set(String key, String obj, Integer day, TimeUnit time) throws Exception {
        if(StringUtils.isBlank(key)){
            return ResultMsg.failure("key不能为空");
        }
        if(null == obj){
            return ResultMsg.failure("值不能为空");
        }
        SysRedisEntity sysRedisEntity = new SysRedisEntity();
        sysRedisEntity.setKeyRedis(key);
        sysRedisEntity.setValueJson(obj);
        sysRedisEntity.setCreateDate(new Date());
        insert(sysRedisEntity);
        stringRedisTemplate.opsForValue().set(key,obj,day,time);
        return ResultMsg.success();
    }

    @Override
    public <T> T getByKeyToPojo(String key, Class<T> tClass) throws Exception {
        if(StringUtils.isBlank(key)){
            return null;
        }
        return JsonUtils.jsonToPojo(stringRedisTemplate.opsForValue().get(key),tClass);
    }

    @Override
    public String get(String key) throws Exception {
        if(StringUtils.isBlank(key)){
            return null;
        }
        return stringRedisTemplate.opsForValue().get(key);
    }

    @Override
    public <T> List<T> getByKeyToList(String key, Class<T> tClass) throws Exception {
        if(StringUtils.isBlank(key)){
            return null;
        }
        String jsonStr = stringRedisTemplate.opsForValue().get(key);
        return JsonUtils.jsonToList(jsonStr,tClass);
    }

    @Override
    public ResultMsg delByIds(String[] ids) throws Exception {
        if(null == ids){
            return ResultMsg.failure(BaseMsg.SEL_NOT_EXISTED);
        }

        for (String id : ids) {
            if(StringUtils.isBlank(id)){
                continue;
            }
            SysRedisEntity sysRedisEntity = sysRedisMapper.selectByPrimaryKey(id);
            if(null != sysRedisEntity && StringUtils.isNotBlank(sysRedisEntity.getKeyRedis())){
                stringRedisTemplate.delete(sysRedisEntity.getKeyRedis());
            }
        }
        return deleteByIds(ids);
    }

    @Override
    public ResultMsg delByKey(String key) throws Exception {
        if(StringUtils.isBlank(key)){
            return ResultMsg.failure("没有要删除的key");
        }
        stringRedisTemplate.delete(key);
        SysRedisDTO dto = new SysRedisDTO();
        dto.setKeyRedis(key);
        sysRedisMapper.delByRedisKey(dto);
        return ResultMsg.success();
    }
}